package com.net.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.googlecode.jsonplugin.annotations.JSON;
import com.net.model.Role;
import com.net.model.User;
import com.net.service.RoleService;
import com.net.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

@Component("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport {
	private User user;
	private Role role;
	private UserService userService;
	private RoleService roleService;
	private List<User> user_list;
	private List<User> user_search_list;
	private Set<Role> role_list;
	private int[] selectedList;
	private int[] selectedRoleList;
	private int start;
	private int limit;
	private int totalProperty;
	private boolean success;
	private String input;
	private String pw;
	private String uname;
	private int uid;
	private int current_selected_id;
	public String list(){
		System.out.println(start+":"+limit+"********");
		user_list = userService.getUsers(start/limit, limit, null, null);
		totalProperty = userService.getCount();
		success = true;
		return SUCCESS;
	}
	public String search(){
		System.out.println(start+":"+limit+"********"+"user name or id:" + user.getName());
		String str = "";
		if("".equals(user.getName())){
			str = "u.id="+user.getId();
		}else if(user.getId()==0){
			str = "u.name like '%"+user.getName()+"%'";
		}else {
			str = "u.name like '%"+user.getName()+"%'" + " or u.id="+user.getId();
		}
		user_list = userService.getUsersByCondition(start/limit, limit, str);
		totalProperty = userService.getCount();
		success = true;
		return SUCCESS;
	}
	
	public String searchRoles(){
		System.out.println("current_selected_id:"+current_selected_id);
		String str = "";
			str = "u.id="+current_selected_id;
		user_list = userService.getUsersByCondition(0,userService.getCount(), str);//改成loadById有问题
		List<Role> roles = new ArrayList<Role>();
		roles.addAll(user_list.get(0).getRoles());
		role_list = new HashSet<Role>();
		for(int i = start; i<start+limit&i<roles.size();i++){
			role_list.add(roles.get(i));
		}
		user_list=null;
		totalProperty = roles.size();
		success = true;
		return SUCCESS;
	}
	
	public String delete(){
		System.out.println("selectedLista:"+selectedList[0]+"length:"+selectedList.length);
		for(int i = 0 ; i < selectedList.length ; i++){
			user = userService.loadUserById(selectedList[i]);
			user.setRoles(null);
			System.out.println(user.getName()+"********");
			userService.delete(user);
		}
		success = true;
		return SUCCESS;
	}
	
	public String deleteRoles(){
		System.out.println("current_selected_id:"+current_selected_id+"length:"+selectedRoleList.length);
		for(int i = 0 ; i < selectedRoleList.length ; i++){
			user = userService.loadUserById(current_selected_id);
			user.getRoles().remove(roleService.loadRoleById(selectedRoleList[i]));
			System.out.println(user.getName()+"********");
			userService.update(user);
		}
		user = null;
		success = true;
		return SUCCESS;
	}
	
	public String add(){
		HttpServletResponse response = ServletActionContext.getResponse();
		  //设置返回数据的编码为UTF-8
		  response.setCharacterEncoding("UTF-8");
		  System.out.println(user.getName()+":name");
		user.setRoles(null);
		userService.add(user);
		System.out.println(user.getId()+":user id");
		success = true;
		return SUCCESS;
	}
	
	public String addRoles(){
		String str = "";
		str = "u.id="+current_selected_id;
		System.out.println("current_selected_id 888:"+current_selected_id);
		user_list = userService.getUsersByCondition(0,userService.getCount(), str);
		user = user_list.get(0);
		System.out.println("user.getName():"+user.getName());
		role_list = user.getRoles();
		for(int i = 0 ; i < selectedRoleList.length ; i++){
			role = roleService.loadRoleById((selectedRoleList[i]));
			role_list.add(role);
		}
		user.setRoles(role_list);
		userService.update(user);
		System.out.println(user.getId()+":user id");
		
		user = null;
		user_list = null;
		user = null;
		role_list = null;
		role = null;
		
		success = true;
		return SUCCESS;
	}
	
	public String update(){
		System.out.println(user.getDisable()+":disable");
		userService.update(user);
		success = true;
		return SUCCESS;
	}
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	@JSON(serialize=false)
	public UserService getUserService() {
		return userService;
	}
	@Resource(name="userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getTotalProperty() {
		return totalProperty;
	}
	public void setTotalProperty(int totalProperty) {
		this.totalProperty = totalProperty;
	}

	public int[] getSelectedList() {
		return selectedList;
	}

	public void setSelectedList(int[] selectedList) {
		this.selectedList = selectedList;
	}

	public List<User> getUser_list() {
		return user_list;
	}

	public void setUser_list(List<User> user_list) {
		this.user_list = user_list;
	}

	public List<User> getUser_search_list() {
		return user_search_list;
	}

	public void setUser_search_list(List<User> user_search_list) {
		this.user_search_list = user_search_list;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getInput() {
		return input;
	}

	public void setInput(String input) {
		this.input = input;
	}

	public String getPw() {
		return pw;
	}

	public void setPw(String pw) {
		this.pw = pw;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public int getCurrent_selected_id() {
		return current_selected_id;
	}
	public void setCurrent_selected_id(int current_selected_id) {
		this.current_selected_id = current_selected_id;
	}
	public Set<Role> getRole_list() {
		return role_list;
	}
	public void setRole_list(Set<Role> role_list) {
		this.role_list = role_list;
	}
	public int[] getSelectedRoleList() {
		return selectedRoleList;
	}
	public void setSelectedRoleList(int[] selectedRoleList) {
		this.selectedRoleList = selectedRoleList;
	}
	@JSON(serialize=false)
	public RoleService getRoleService() {
		return roleService;
	}
	@Resource(name="roleService")
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}

}
